package com.thingple.web.bridge.message;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.thingple.h5.bridge.AbstractJsBridge;
import com.thingple.h5.bridge.BridgeConfig;
import com.thingple.message.MQTTMessage;
import com.thingple.message.MessageService;

@BridgeConfig(moduleName = "message")
public class MessageJsBridgeImpl extends AbstractJsBridge implements MessageJsBridge {

    public MessageJsBridgeImpl(WebView webView, Context context) {
        super(webView, context);
    }

    @JavascriptInterface
    @Override
    public void send(String content, String destination) {

        if (content == null || destination == null) {
            return;
        }
        Log.d(getClass().getName() + "#send", "Js message 收到推送:" + content + " 目的地:" + destination);
        MQTTMessage message = new MQTTMessage();
        message.content = content;
        message.topic = destination;
        MessageService messageService = MessageService.shareInstance();
        if (messageService != null) {
            messageService.sendMessage(message);
        } else {
            Log.d(getClass().getName() + "#send", "MessageService 为null");
        }
    }

    @Override
    public void reset() {
        // TODO
    }

    @Override
    public void destroy() {

        // 啥也不做,因为没有临时变量
    }
}
